package com.baizhi.controller;

import com.baizhi.common.Constant;
import com.baizhi.entity.Employee;
import com.baizhi.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @RequestMapping("select")
    public Map select(Integer page,Integer limit,String emsName,Integer deptId,Integer comId){
        PageInfo<Employee> pageInfo = employeeService.select(page, limit, emsName, deptId, comId);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    @RequestMapping("upload")
    public Map upload(MultipartFile file){//头像上传的方法
        Map map = new HashMap();
        String filename = file.getOriginalFilename();//获取当前上传的原文件名
        String newFilename = UUID.randomUUID().toString()+filename;//生成头像文件上传后的新文件名  保存到数据库中的名字
        try {
            file.transferTo(new File(Constant.IMG_LOCATION,newFilename));//将文件上传的虚拟目录Constant.IMG_LOCATION中
            map.put("fileName",newFilename);//将上传后的文件名保存到map中  响应给页面用于添加操作
        } catch (IOException e) {
            map.put("fileName",null);
        }
        return map;
    }

    @RequestMapping("add")
    public Map addEmployee(Employee employee){
        Map map = new HashMap();
        try {
            employeeService.insert(employee);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    public Map removeEmployee(Integer emsId){
        Map map = new HashMap();
        try {
            employeeService.delete(emsId);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("removeAny")
    public Map removeAnyEmployee(Integer[] emsIds){
        Map map = new HashMap();
        try {
            employeeService.deleteAny(emsIds);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }

    @RequestMapping("getOne")
    public Employee getOneEmployee(Integer emsId){
        return employeeService.selectById(emsId);
    }
    @RequestMapping("update")
    public Map updateEmployee(Employee employee){
        System.out.println(employee);
        Map map = new HashMap();
        try {
            employeeService.update(employee);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
}
